﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YFINGenerator
{
    public partial class Factory
    {
        public static string ModelGenerator(EntityInfo entityInfo)
        {
            string v = entityInfo.Name.Remove(0, 5);
            v = v.Insert(1, v[0].ToString().ToLower());
            v = v.Remove(0, 1);
            string res =
                @"//
//此代码由YFINGenerator自动生成
//Copyright (c) 段凯强 2013~2014
//" + DateTime.Now + @"
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using YFIN.Data;

namespace YFIN.Models
{
    /// <summary>
    /// " +
                entityInfo.Remark + @"
    /// </summary>
    [DataContract]
    public class " +
                entityInfo.Name.Replace("YFIN_", "Model") + @"
    {";
            foreach (var a in entityInfo.Attributes)
            {
                res += @"
        [DataMember]
        public " + a.Substring(a.IndexOf(':') + 1) + " " +
                       a.Substring(0, a.IndexOf(':')) + @" { get; set; }
";
            }
            foreach (var a in entityInfo.AttrsNull)
            {
                res += @"
        [DataMember]
        public " + a.Substring(a.IndexOf(':') + 1) + " " +
                       a.Substring(0, a.IndexOf(':')) + @" { get; set; }
";
            }
            res +=
                @"
        public static " + entityInfo.Name.Replace("YFIN_", "Model") + @" ConvertTo" +
                entityInfo.Name.Replace("YFIN_", "Model") + @"(" + entityInfo.Name +
                @" " + v + @")
        {
            return " + v + @" == null ? null : new " + entityInfo.Name.Replace("YFIN_", "Model") +
                @"
                       {
";
            foreach (var a in entityInfo.Attributes)
            {
                res += "                           " + a.Substring(0, a.IndexOf(':')) + " = " + v + @"." +
                       a.Substring(0, a.IndexOf(':')) + ",\r\n";
            }
            foreach (var a in entityInfo.AttrsNull)
            {
                res += "                           " + a.Substring(0, a.IndexOf(':')) + " = " + v + @"." +
                       a.Substring(0, a.IndexOf(':')) + ",\r\n";
            }
            res += @"                       };
        }

        public static " + entityInfo.Name + @" ConvertTo" +
                   entityInfo.Name + @"(" + entityInfo.Name.Replace("YFIN_", "Model") +
                   @" " + v + @")
        {
            return " + v + @" == null ? null : new " + entityInfo.Name + @"
            {
";
            foreach (var a in entityInfo.Attributes)
            {
                res += "                           " + a.Substring(0, a.IndexOf(':')) + " = " + v + @"." +
                       a.Substring(0, a.IndexOf(':')) + ",\r\n";
            }
            foreach (var a in entityInfo.AttrsNull)
            {
                res += "                           " + a.Substring(0, a.IndexOf(':')) + " = " + v + @"." +
                       a.Substring(0, a.IndexOf(':')) + ",\r\n";
            }
            res += @"            };
        }

        public static void CopyTo" + entityInfo.Name + @"(" +
                   entityInfo.Name + @" " + v + @"1, " + entityInfo.Name.Replace("YFIN_", "Model") +
                   @" " + v + @"2)
        {
";
            foreach (var a in entityInfo.Attributes)
            {
                if (a.Substring(0, a.IndexOf(':')) == entityInfo.Key)
                {
                    continue;
                }
                res += "            " + v + @"1." + a.Substring(0, a.IndexOf(':')) + " = " + v + @"2." +
                       a.Substring(0, a.IndexOf(':')) + ";\r\n";
            }
            foreach (var a in entityInfo.AttrsNull)
            {
                res += "            " + v + @"1." + a.Substring(0, a.IndexOf(':')) + " = " + v + @"2." +
                       a.Substring(0, a.IndexOf(':')) + ";\r\n";
            }

            res += @"        }
    }
}";
            return res;
        }
    }
}
